﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.ServiceModel;
using FlickrSL.Data;
using SilverlightRestServices.Description;

namespace FlickrSL
{
    public partial interface Flickr
    {
        [OperationContract(AsyncPattern = true)]
        [RestGet("services/rest/?method=flickr.photosets.getInfo&photoset_id={0}")]
        IAsyncResult BeginPhotosets_GetPhotosetInfo(Int64 photosetId, AsyncCallback callback, Object state);

        FlickrResponse<PhotosetInfo> EndPhotosets_GetPhotosetInfo(IAsyncResult result);

        [OperationContract(AsyncPattern = true)]
        [RestGet("services/rest/?method=flickr.photosets.getList&user_id={0}")]
        IAsyncResult BeginPhotosets_GetPhotosets(String userId, AsyncCallback callback, Object state);

        FlickrResponse<PhotosetCollection> EndPhotosets_GetPhotosets(IAsyncResult result);


        [OperationContract(AsyncPattern = true)]
        [RestGet("services/rest/?method=flickr.photosets.getList")]
        IAsyncResult BeginPhotosets_GetMyPhotosets(AsyncCallback callback, Object state);

        FlickrResponse<PhotosetCollection> EndPhotosets_GetMyPhotosets(IAsyncResult result);

        [OperationContract(AsyncPattern = true)]
        [RestGet("services/rest/?method=flickr.photosets.getPhotos&photoset_id={0}")]
        IAsyncResult BeginPhotosets_GetPhotos(Int64 photosetId, AsyncCallback callback, Object state);

        FlickrResponse<Photoset> EndPhotosets_GetPhotos(IAsyncResult result);

        [OperationContract(AsyncPattern = true)]
        [RestGet("services/rest/?method=flickr.photosets.getPhotos&photoset_id={0}&media={1}&privacy_filter={2}&extras={3}")]
        IAsyncResult BeginPhotosets_GetPhotosEx(Int64 photosetId, MediaFilter mediaFilter, PrivacyFilter privacyFilter, PhotoFieldCollection extraFields, AsyncCallback callback, Object state);

        FlickrResponse<Photoset> EndPhotosets_GetPhotosEx(IAsyncResult result);

        [OperationContract(AsyncPattern = true)]
        [RestGet("services/rest/?method=flickr.photosets.getPhotos&photoset_id={0}&page={1}&per_page={2}")]
        IAsyncResult BeginPhotosets_GetPhotosPage(Int64 photosetId, Int32 page, Int32 perPage, AsyncCallback callback, Object state);

        FlickrResponse<Photoset> EndPhotosets_GetPhotosPage(IAsyncResult result);

        [OperationContract(AsyncPattern = true)]
        [RestGet("services/rest/?method=flickr.photosets.getPhotos&photoset_id={0}&media={1}&privacy_filter={2}&extras={3}&page={4}&per_page={5}")]
        IAsyncResult BeginPhotosets_GetPhotosPageEx(Int64 photosetId, MediaFilter mediaFilter, PrivacyFilter privacyFilter, PhotoFieldCollection extraFields, Int32 page, Int32 perPage, AsyncCallback callback, Object state);

        FlickrResponse<Photoset> EndPhotosets_GetPhotosPageEx(IAsyncResult result);
    }

    [TypeConverter(typeof(MediaFilterTypeConverter))]
    public enum MediaFilter
    {
        All,
        Photos,
        Videos
    }

    public class MediaFilterTypeConverter : TypeConverter
    {
        public override Boolean CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(String))
            {
                return true;
            }
            else
            {
                return base.CanConvertTo(context, destinationType);
            }
        }

        public override Object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType)
        {
            if (destinationType == typeof(String))
            {
                switch ((MediaFilter)value)
                {
                    case MediaFilter.All:
                        return "all";
                    case MediaFilter.Photos:
                        return "photos";
                    case MediaFilter.Videos:
                        return "videos";
                    default:
                        throw new InvalidOperationException("MediaFilter Value Not Recognized");
                }
            }
            else
            {
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }
    }

    [TypeConverter(typeof(PrivacyFilterTypeConverter))]
    public enum PrivacyFilter
    {
        Public = 1,
        Friends,
        Family,
        FriendsAndFamily,
        Private
    }

    public class PrivacyFilterTypeConverter : TypeConverter
    {
        public override Boolean CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(String))
            {
                return true;
            }
            else
            {
                return base.CanConvertTo(context, destinationType);
            }
        }

        public override Object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType)
        {
            if (destinationType == typeof(String))
            {
                return ((Int32)value).ToString();
            }
            else
            {
                return base.ConvertTo(context, culture, value, destinationType);
            }
        }
    }
}
